﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using Ylh.BLL;
using System.Data;

public partial class Module_CustManger_AddCust : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button2.OnClientClick = ActiveWindow.GetHideReference();
    }
    protected void CustName_TextChanged(object sender, EventArgs e)
    {
        
        BindGVList(GetCondition());
        
    }
    protected void CompanyName_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(CustName.Text))
        {
            Alert.Show("客户名称不能为空");
            return;
        }
        BindGVList(GetCondition());
       
    }
    protected void TelPhone_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(CustName.Text))
        {
            Alert.Show("客户名称不能为空");
            return;
        }
        BindGVList(GetCondition());
       
    }
    protected void CustAddress_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(CustName.Text))
        {
            Alert.Show("客户名称不能为空");
            return;
        }
        BindGVList(GetCondition());
        
    }
    protected void Legal_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(CustName.Text))
        {
            Alert.Show("客户名称不能为空");
            return;
        }
        BindGVList(GetCondition());
        
    }


    private string GetCondition()
    {
        string ConditionStr = string.Empty;

        if (!string.IsNullOrEmpty(CustName.Text) && string.IsNullOrEmpty(CustAddress.Text))
        {
            ConditionStr += "  CustName ='" + CustName.Text + "' ";
        }
        else if (!string.IsNullOrEmpty(CustName.Text) && !string.IsNullOrEmpty(CustAddress.Text))
        {
            ConditionStr += " ( CustName ='" + CustName.Text + "'  and CustAddress ='" + CustAddress.Text + "' ) ";
        }
       

        if (!string.IsNullOrEmpty(CompanyName.Text))
        {
            ConditionStr += " or CompanyName ='" + CompanyName.Text + "' ";
        }


        if (!string.IsNullOrEmpty(TelPhone.Text))
        {
            ConditionStr += " or TelPhone ='" + TelPhone.Text + "' ";
        }


        //if (!string.IsNullOrEmpty(CustAddress.Text))
        //{
        //    ConditionStr += " and CustAddress ='" + CustAddress.Text + "' ";
        //}


        if (!string.IsNullOrEmpty(Legal.Text))
        {
            ConditionStr += " or Legal ='" + Legal.Text + "' ";
        }
        return ConditionStr;
        
    }

    private bool BindGVList(string condition)
    {
        Ylh.BLL.CustBLL bllc = new CustBLL(DbConnectionStr);
        DataTable dt;
        Grid1.DataSource = dt = bllc.GetCustByCodition(condition);
        Grid1.DataBind();

        if (dt.Rows.Count > 0) return true;
        return false;
    }
    protected void BtnCheck_Click(object sender, EventArgs e) {    }


    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (string .IsNullOrEmpty(CustName.Text))
        {
            Alert.Show("客户名称不能为空");
            return;
        }


        if (BindGVList(GetCondition()))
        {
            Alert.ShowInParent("这个客户已经被其他同事保护了,请重新填写");
            return;
        }
        CustBLL bllc = new CustBLL(DbConnectionStr);
        bllc.InsertInto(CustName.Text, StaffID.ToString(), "true", CompanyName.Text.Trim(), TelPhone.Text, CustAddress.Text.Trim(), Legal.Text);
        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
    }
}